package com.example.shoppingmallspringboot.controller;

import com.example.shoppingmallspringboot.service.OrdersService;
import com.example.shoppingmallspringboot.util.JwtConstant;
import com.example.shoppingmallspringboot.util.JwtUtil;
import com.example.shoppingmallspringboot.vo.OrdersDetailGoodsVo;
import com.example.shoppingmallspringboot.vo.OrdersVo;
import com.example.shoppingmallspringboot.util.FwResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@Tag(name = "OrdersController",description = "用户订单控制器")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    @GetMapping("/userOrders/{userId}")
    @Operation(summary = "获取用户订单商品",description = "用户id为参数")
    public FwResult getUserOrders(@PathVariable("userId") Integer userId){
        List<OrdersDetailGoodsVo> ordersDetailGoodsVoList = ordersService.getUserOrders(userId);
        FwResult<List<OrdersDetailGoodsVo>> result = FwResult.ok(ordersDetailGoodsVoList,"用户订单");
        return result;
    }
    @PostMapping("/orders/handle")
    @Operation(summary = "订单结算",description = "购物车id、用户id、商品id、商品数量,用户地址为参数")
    public FwResult ordersHandle(@RequestBody List<OrdersVo> ordersVoList){

        try{
            ordersService.orders(ordersVoList);
            return FwResult.ok("结算成功");
        } catch (Exception e) {
            return FwResult.failedMsg(e.getMessage());
        }
    }
    @PostMapping("/orders/delete")
    @Operation(summary = "删除订单",description = "订单id为参数")
    public FwResult deleteOrder(@RequestBody Integer orderId){
        return ordersService.deleteOrder(orderId);
    }
}
